package comman.duke 
{
	import comman.duke.BlinkItem;
	import comman.duke.FrameItem;
	import comman.duke.PoolMgr;
	import flash.filters.GlowFilter;
	/**
	 * ...
	 * @author jerry.d
	 */
	public class BlinkMgr 
	{
		private var glowFilter:GlowFilter = new GlowFilter(0xcccccc, 1, 6, 6, 1, 1);
		
		private var frameItem:FrameItem;
		public var name:String;
		public function BlinkMgr() 
		{
			this.name = "blinkmgr";
			frameItem = new FrameItem(this.name, this.update);
			items = new Vector.<BlinkItem>();
		}
		
		private var items:Vector.<BlinkItem>;
		public function add(item:BlinkItem):void{
			if ( items.indexOf(item) == -1){
				items.push(item);
				item.display.filters = [glowFilter];
			}
			if ( !FrameMgr.Instance.has(this.name)){
				FrameMgr.Instance.add(this.frameItem);
			}
		}
		
		public function remove(item:BlinkItem):void{
			var index:int = items.indexOf(item);
			if ( index != -1){
				items.splice(index, 1);
				item.display.filters = [];
				PoolMgr.reclaim(item);
			}
			if ( items.length == 0){
				FrameMgr.Instance.remove(this.name);
			}
		}
		
		public function removeByName(itemName:String):void{
			for each(var item:BlinkItem in items){
				if ( item.name == itemName){
					this.remove(item);
					break;
				}
			}
		}
		/**
		 * true 增强
		 * false 衰弱
		 * */
		private var flag:Boolean = true;
		public function update(delta:int):void{
			if ( flag ){
				glowFilter.strength += Math.ceil((12 - glowFilter.strength) / 3);
				if ( glowFilter.strength == 12){
					flag = !flag;
				}
			}else{
				glowFilter.strength -= Math.floor((glowFilter.strength - 1 ) / 3);
				if ( glowFilter.strength == 1){
					flag = !flag;
				}
				for each(var item:BlinkItem in items){
					if ( item.times != -1){
						item.currentTimes++;
						if ( item.currentTimes >= item.times ){
							this.remove(item);
						}
					}
				}
			}
		}
		
		private static var _instance:BlinkMgr;
		public static function get Instance():BlinkMgr{
			if ( BlinkMgr._instance == null){
				BlinkMgr._instance = new BlinkMgr();
			}
			return BlinkMgr._instance;
		}
	}
}